const {
  User
} = require("../../model/user");
const bcrypt = require('bcrypt');
const userEdit = async (req, res) => {
  const {
    id,
    username,
    email,
    role,
    state,
    password
  } = req.body;
  let user = await User.findOne({
    _id: id
  });
  const isValid = await bcrypt.compare(password, user.password);
  if (isValid) {
    // 将用户信息更新到数据库中
    await User.updateOne({
      _id: id
    }, {
      username: username,
      email: email,
      role: role,
      state: state
    });
    let newUser = await User.findOne({
      _id: id
    });
    res.send({
      code: 200,
      data: {
        userInfo: newUser,
      },
      msg: "修改成功"
    })
  } else {
    // 密码错误
    res.send({
      code: 400,
      data: {},
      msg: "密码错误，修改失败"
    })
  }
}

module.exports = userEdit;